package com.test;

import com.connection.DataConnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UserActivity extends Activity {
	
	private Button login;
	private EditText userName, password;
	static String tmpName;//............................................................
	
	DataConnection data = new DataConnection(this);
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);
        
        data.open();
        
        userName = (EditText) findViewById(R.id.txt_username);
		password = (EditText) findViewById(R.id.txt_password);
	
		
		
		login = (Button) findViewById(R.id.register);
		login.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub  
				  data.open();
				Cursor c1 = data.db.query("User", null, null, null, null,
						null, null);

				int indexName = c1.getColumnIndex("userName");
				int indexPwd = c1.getColumnIndex("password");
				try {

					for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {
						
						if (c1.getString(indexName).equals(userName.getText().toString()) 
								&& c1.getString(indexPwd).equals(password.getText().toString())) {
							tmpName = userName.getText().toString();
							startActivity(new Intent(UserActivity.this,
									ProductActivity.class));
							
						}else{
							System.out.println("error problem");
							AlertDialog.Builder builder = new AlertDialog.Builder(
									UserActivity.this);
							builder.setTitle("Error")
									.setMessage(
											"Your information are not correct. \n Please try again!");
							builder.setNegativeButton("back",
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface arg0, int arg1) {
											// TODO Auto-generated method stub
											// finish();
										}
									}).create().show();
						}
					}
				} catch (Exception e) {

				}
				data.close();
				
			}
			
		});
		
		

    }
}
